package 二叉树;

public class Solution222 {
    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(1);
        TreeNode treeNode1 = new TreeNode(2);
        TreeNode treeNode2 = new TreeNode(3);
        treeNode.left = treeNode1;
        treeNode.right = treeNode2;
        System.out.println(countNodes(treeNode));
    }

    public static int countNodes(TreeNode root) {
        return getNum(root);
    }

    /**
     * 获取节点下有多少个根节点
     * root 当前节点
     *
     * @param root
     * @return
     */
    public static int getNum(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = getNum(root.left);
        int right = getNum(root.right);
        return left + right + 1;
    }


}
